/**
 * Project Name:sq580_ow;<br/>
 * File Name:UserDaoImpl;<br/>
 * Package Name:com.sq580.ow.dao.impl;<br/>
 * Date: 2018-06-22 14:07;<br/>
 * Copyright (c) 2018, www.sq580.com All Rights Reserved.;<br/>
 */
package com.sq580.ow.dao.impl;

import com.sq580.ow.bean.User;
import com.sq580.ow.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * ClassName: UserDaoImpl<br/>
 * Function: TODO ADD FUNCTION(可选)<br/>
 * Reason: TODO ADD REASON(可选)<br/>
 * Date: 2018-06-22 14:07<br/>
 *
 * @author chenbang
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public User getUserById(Integer id) {
        List<User> list = jdbcTemplate.query("select * from tb_user where id = ?",new Object[]{id},new BeanPropertyRowMapper<>(User.class));
        if(list != null && list.size()>0){
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<User> getUserList() {
        List<User> list = jdbcTemplate.query("select * from tb_user", new Object[]{},new BeanPropertyRowMapper<>(
                User.class
        ));
        if(list !=null && list.size()>0){
            return list;
        }else {
            return null;
        }
    }

    @Override
    public int add(User user) {
        return jdbcTemplate.update("insert into tb_user(username,age,ctm) values(?,?,?)",user.getUsername(),user.getAge(),new Date());
    }

    @Override
    public int update(Integer id, User user) {
        return jdbcTemplate.update("UPDATE tb_user set username=?,age =? where id = ?",user.getUsername(),user.getAge(),id);
    }

    @Override
    public int delete(Integer id) {
        return jdbcTemplate.update("DELETE from tb_user where id = ?", id);
    }
}